﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using AutoPoco.DataSources;
using AutoPoco.Engine;
using DefaultConnection;
using MementoWeb.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using AutoPoco;

namespace MementoWeb.Tests.Integration
{
    /// <summary>
    /// Summary description for WorkLogDataTest
    /// </summary>
    [TestClass]
    public class WorkLogDataTest
    {
        private IGenerationSessionFactory _factory = null;
        
        public WorkLogDataTest()
        {
            _factory = AutoPocoContainer.Configure(x =>
            {
                x.Conventions(c =>
                {
                    c.UseDefaultConventions();
                });
                x.AddFromAssemblyContainingType<WorkLog>();
                x.Include<Project>()
                    .Setup(c => c.Description).Use<RandomStringSource>(5, 20)
                    .Setup(c => c.DateCreated).Value(DateTime.UtcNow)
                    .Setup(c => c.DateUpdated).Value(DateTime.UtcNow);
                x.Include<Task>()
                    .Setup(c => c.Description).Use<RandomStringSource>(5, 20)
                    .Setup(c => c.DateCreated).Value(DateTime.UtcNow)
                    .Setup(c => c.DateUpdated).Value(DateTime.UtcNow);
                x.Include<Person>()
                    .Setup(c => c.DateCreated).Value(DateTime.UtcNow)
                    .Setup(c => c.DateUpdated).Value(DateTime.UtcNow);
                x.Include<WorkLog>()
                    .Setup(c => c.Description).Use<RandomStringSource>(5, 20)
                    .Setup(c => c.DateCreated).Value(DateTime.UtcNow)
                    .Setup(c => c.DateUpdated).Value(DateTime.UtcNow)
                    .Setup(c => c.Workdate).Use<DateOfBirthSource>();
            });
        }

        [TestInitialize]
        public void SetupTest()
        {
            
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void WorkLog_CreateNew_AddsOneRecord()
        {
            // Arrange
            var dataRepository = new DataRepository();
            var session = _factory.CreateSession();
            var workLog = session.Single<WorkLog>().Get();
            var project = session.Single<Project>().Get();
            project.Insert();
            var task = session.Single<Task>().Get();
            task.ProjectId = project.ProjectId;
            task.Insert();
            var person = session.Single<Person>().Get();
            person.Insert();
            workLog.PersonId = person.PersonId;
            workLog.TaskId = task.TaskId;
            // Act
            int previousWorklogs = dataRepository.GetAllWorkLogs().Count();
            dataRepository.CreateNewWorkLog(workLog);
            int newWorklogs = dataRepository.GetAllWorkLogs().Count();

            // Assert
            Assert.AreEqual(newWorklogs, previousWorklogs + 1);
        }
    }
}
